from collections import defaultdict


class Solution:
    def topKFrequent(self, nums: list, k: int) -> list:
        mid_dic = defaultdict(int)
        for num in nums:
            mid_dic[num] += 1
        res_list = [i for i in mid_dic]
        res_list.sort(key=lambda i: mid_dic[i], reverse=True)
        return res_list[:k]


a = Solution()
print(a.topKFrequent(nums=[1, 1, 1, 2, 2, 3], k=2))
print(a.topKFrequent(nums=[1], k=1))
